import Vue from "vue";
import store from "@/store";
import DataDict from "@/utils/dict";
import { getDicts as getDicts } from "@/api/system/dict/data";
import request from "@/utils/request.js";

function searchDictByKey(dict, key) {
  if (key == null && key == "") {
    return null;
  }
  try {
    for (let i = 0; i < dict.length; i++) {
      if (dict[i].key == key) {
        return dict[i].value;
      }
    }
  } catch (e) {
    return null;
  }
}

function install() {
  Vue.use(DataDict, {
    metas: {
      "*": {
        labelField: "dictLabel",
        valueField: "dictValue",
        request(dictMeta) {
          const storeDict = searchDictByKey(store.getters.dict, dictMeta.type);
          if (storeDict) {
            return new Promise((resolve) => {
              resolve(storeDict);
            });
          } else {
            return new Promise((resolve, reject) => {
              getDicts(dictMeta.type)
                .then((res) => {
                  store.dispatch("dict/setDict", {
                    key: dictMeta.type,
                    value: res.data,
                  });
                  resolve(res.data);
                })
                .catch((error) => {
                  reject(error);
                });
            });
          }
        },
      },
    },
  });
}

/**
 * 查询业务类型
 */
async function queryBusinessType() {
  try {
    let res = await request({
      url: "/system/change/queryBusinessType",
      method: "get",
    });
    return res.data.map((item) => item); // 直接返回数组
  } catch (error) {
    console.error("Error querying business type:", error); // 添加错误日志，帮助调试
    return [];
  }
}

/**
 * 商品等级：1vip 3共享店主 4联创 5区域运营 6总裁
 */
const levelList = [
  {
    name: "VIP",
    value: 1,
  },
  {
    name: "共享店主",
    value: 3,
  },
  {
    name: "联创",
    value: 4,
  },
  {
    name: "区域运营",
    value: 5,
  },
  {
    name: "总裁",
    value: 6,
  },
];
/**
 * 代理等级：3共享店主 4联创 5区域运营 6总裁
 */
const memberLevelList = [
  {
    name: "共享店主",
    value: 3,
  },
  {
    name: "联创",
    value: 4,
  },
  {
    name: "区域运营",
    value: 5,
  },
  {
    name: "总裁",
    value: 6,
  },
];
/**
 * 会员等级
 */
const memberList = [
  {
    name: "VIP",
    value: 1,
  },
  {
    name: "SVIP",
    value: 2,
  },
  {
    name: "共享店主",
    value: 3,
  },
  {
    name: "联创",
    value: 4,
  },
  {
    name: "区域运营中心",
    value: 5,
  },
  {
    name: "总裁",
    value: 6,
  },
];

/**
 * 会员购买权限
 */
const memberPurchase = [
  // {
  //   name: "普通",
  //   value: 0,
  // },
  {
    name: "VIP",
    value: 1,
  },
  {
    name: "SVIP",
    value: 2,
  },
  {
    name: "共享店主",
    value: 3,
  },
  {
    name: "联创",
    value: 4,
  },
  {
    name: "区域运营中心",
    value: 5,
  },
  {
    name: "总裁",
    value: 6,
  },
];

/**
 * 分销提成类型
 */
const distribution = [
  {
    name: "直推",
    value: 1,
  },
  {
    name: "间推",
    value: 2,
  },
  {
    name: "单单奖",
    value: 3,
  },
  {
    name: "平级推奖",
    value: 4,
  },
  {
    name: "跨级推奖",
    value: 5,
  },
  {
    name: "团队奖",
    value: 7,
  },
];

/**
 * 积分抵扣类型
 */
const integralDeduction = [
  {
    name: "固定值",
    value: 1,
  },
  {
    name: "百分比",
    value: 2,
  },
  {
    name: "单间抵扣",
    value: 3,
  },
  {
    name: "多件累计抵扣",
    value: 4,
  },
  {
    name: "按数量抵扣",
    value: 5,
  },
];

export default {
  install,
  levelList,
  memberLevelList,
  memberList,
  distribution,
  memberPurchase,
  integralDeduction,
  queryBusinessType,
};
